package com.house.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.house.common.Result;
import com.house.entity.House;
import com.house.entity.Lease;
import com.house.service.DictService;
import com.house.service.HouseService;
import com.house.service.LeaseService;
import com.house.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 租赁管理
 */
@Controller
@RequestMapping("lease")
public class LeaseController {

    @Autowired
    LeaseService leaseService;

    @Autowired
    HouseService houseService;

    @Autowired
    DictService dictService;


    /**
     * 租赁信息管理页
     * @return
     */
    @GetMapping("manage")
    public String manage() {
        return "admin/leaseManage";
    }

    @PostMapping("manage/list")
    @ResponseBody
    public List<Lease> list(Lease lease) {
        return leaseService.list(Wrappers.<Lease>query().orderByDesc("create_time"));
    }


    /**
     * 添加/更新租赁信息  有ID为更新
     * @param lease
     * @return
     */
    @PostMapping("manage/saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(Lease lease){
        if(StrUtil.isEmpty(lease.getId())) {
            lease.init();
        } else {
            lease.setUpdateTime(DateUtil.now());
        }

        leaseService.saveOrUpdate(lease);
        return Result.ok();
    }


    /**
     * 租赁信息
     * @param id
     * @return
     */
    @PostMapping("/info")
    @ResponseBody
    public Result info(String id){
        Lease lease = leaseService.getById(id);
        return Result.ok(lease);
    }


    /**
     * 删除租赁信息
     * @param id
     * @return
     */
    @PostMapping("/manage/delete")
    @ResponseBody
    public Result delete(@RequestBody List<String> id){
        leaseService.removeByIds(id);
        return Result.ok();
    }


    /**
     * 分页查询租赁信息
     * @param lease
     * @return
     */
    @PostMapping("/public/findByPage")
    @ResponseBody
    public Result findByPage(Lease lease) {
        if(lease.getPriceMin() != null && lease.getPriceMin() <= 0) {
            lease.setPriceMin(null);
        }

        if(lease.getPriceMax() != null && lease.getPriceMax() <= 0) {
            lease.setPriceMax(null);
        }
        List<Lease> leases = leaseService.findByPage(lease);
        return Result.ok(leases);
    }


    /**
     * 获取有效信息条数
     * @param lease
     * @return
     */
    @PostMapping("/public/getValidCount")
    @ResponseBody
    public Result getValidCount(Lease lease) {
        if(lease.getPriceMin() != null && lease.getPriceMin() <= 0) {
            lease.setPriceMin(null);
        }

        if(lease.getPriceMax() != null && lease.getPriceMax() <= 0) {
            lease.setPriceMax(null);
        }
        int count = leaseService.getValidCount(lease);
        return Result.ok(count);
    }



    /**
     * 详细信息
     * @return
     */
    @GetMapping("/public/detail/{id}")
    public String detail(HttpServletRequest request, Model model, @PathVariable("id") String id) {
        Lease lease = leaseService.getById(id);
        House house = houseService.getById(lease.getHouseId());
        house.setPosition(dictService.getText("position", house.getPosition()));
        house.setOrientation(dictService.getText("orientation", house.getOrientation()));
        house.setType(dictService.getText("houseType", house.getType()));

        houseService.imageProcessor(house);

        lease.setHouse(house);
        model.addAttribute("data", lease);
        model.addAttribute("user", LoginUtil.getLoginUser());

        if(LoginUtil.checkAgentIsMobile(request)) {
            return "mobile/detail";
        } else {
            return "pc/detail";
        }

    }



    @GetMapping("/create")
    @ResponseBody
    public Result create() {
        if(!"admin".equals(LoginUtil.getLoginUser().getUsername())) {
            return Result.error();
        }
        leaseService.createTestData();
        return Result.ok();
    }
}
